#include "condvar.c"
#include "rwlock.c"
#include <pthread.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void * produce0_9(void * in) {
  for (char i = '0'; i < '9'; i++) {
    struct msg *mp = malloc(sizeof(struct msg));
    mp->c = &i;
    printf("thread id = %d produce %c\n",(unsigned int)pthread_self(),i);
    enqueue_msg(mp);
  }
  int *result = malloc(sizeof(int));
  *result=0;
  return result;
}


// 线程间的条件等待，相当于java中的 wait() notify()
int main() {

  pthread_t t1, t2, t3, t4, t5;

  void		*exit_result;


// 其中第三个参数void *(*__start_routine) (void *)
// 只能接受 void* aa(void * args); 类型的方法，不能将指针类型从 void* 改为其他任何类型，不然编译器会警告
  pthread_create(&t1, NULL, process_msg, NULL);

  pthread_create(&t2, NULL, process_msg, NULL);

  pthread_create(&t3, NULL, process_msg, NULL);

  pthread_create(&t4, NULL, produce0_9, NULL);

  pthread_create(&t5, NULL, produce0_9, NULL);


  pthread_join(t4, exit_result);
  pthread_join(t5, exit_result);

  sleep(3);

  return 0;
}
// 读写锁的测试
int read_write_lock_test() {

  struct queue *que = malloc(sizeof(struct queue));

  queue_init(que);

  struct job *play = malloc(sizeof(struct job));

  play->name = "玩耍";

  job_insert(que, play);

  struct job *music = malloc(sizeof(struct job));

  music->name = "听音乐";

  job_insert(que, music);

  free(que);
  return 0;
}